package com.sheng.center.util;

import java.util.List;

public class Pager<T> {
	
	private Integer totalPage; //总页数
	private Integer currentPage; //当前页码
	private Integer totalRecord; //总记录数
	private Integer pageSize;    //每页记录数
	public List<T> list;
	private boolean flag;

	public Pager() {
		super();
	}
	public Pager(Integer pageSize,Integer pageNo,List<T> list){
		//传入三个参数。第一个是每页记录数（可以写死）,第二个为未来跳到
		//的页面数（要从页面传），第三个为数据库某个表全部信息
		if(list == null || list.isEmpty()||list.size()==0){
			this.flag = list.isEmpty();
			return;
		}
		this.totalRecord = list.size();
		this.pageSize = pageSize;
		this.list = list;
		//总页数=总记录数%每页记录数，若为整数，则等于它，否则总页数+1 
		//假设总记录数totalRecord=100,totalPage=10
		this.totalPage = this.totalRecord%this.pageSize == 0?this.totalRecord/this.pageSize:(this.totalRecord/this.pageSize)+1;
		//当前第几页
		//假设pageNo=3
		if(this.totalPage<pageNo){
			this.currentPage = this.totalPage;
		}else{
			this.currentPage = pageNo;
		}
		//起始索引=每页记录数*当前页-1
		//startIndex = 2*（3-1）=20
		Integer startIndex = this.pageSize*(this.currentPage-1);
		//结束索引=每页记录数*当前页>总记录数?总记录数:每页记录数*当前页
		//endIndex = 2*3=30
		Integer endIndex = this.pageSize*this.currentPage>this.totalRecord?this.totalRecord:this.pageSize*this.currentPage;
		//list 返回给dao层
		this.list = list.subList(startIndex, endIndex);
	}
	public Integer getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public Integer getTotalRecord() {
		return totalRecord;
	}
	public void setTotalRecord(Integer totalRecord) {
		this.totalRecord = totalRecord;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	

}